$(document).ready(function () {
    $('body').height($('body')[0].clientHeight);
    var all = $('body')[0].clientHeight;
    var header = $('.bar-nav').height();
    var foot = $('.to_bottom').height();
    var tishi = $('.content-padded').height();
    $('.ant-form-inline').height(all - header - foot - tishi);

});
$.init();
checkLogin();
var work_info = getCache(login_key);
var mid = GetQueryString('mid');
var dataArr = [];
var title = '';
var title1 = '';
var title2 = '';
var num = '';
var num1 = '';
var num2 = '';
var showcontent = '';
var showcontent1 = '';
var showcontent2 = '';
$("#bucket_category_id_value").change(function () {
    if (dataArr.length > 0) {
        for (var i = 0; i < dataArr.length; i++) {
            if (dataArr[i]['title'] == $("#bucket_category_id_value").val()) {
                title = dataArr[i]['title'];
                var bucket_category_id = $("#bucket_category_id").val(dataArr[i]['bucket_category_id']);
            }
        }
    }
});

var obj = new Object();
obj.list = new Array();

var success = function(res){
    var rdataArr_list = res.data.bucket_list;

    dataArr = rdataArr_list.splice(1);
    var values = [];
    for(var i = 0;i<dataArr.length;i++){
        values[i] =  dataArr[i].title;
    }


    var html = template('bucket_detail',obj);
    document.getElementById('bucket_detail_content').innerHTML = html;
    var index = 0;

    $(".picker").picker({
        toolbarTemplate: '<header class="bar bar-nav">\
  <button class="button button-link pull-left  close-picker" style="color:#0894ec ;">取消</button>\
  <button class="button button-link pull-right close-picker" style="color:#0894ec ;">确定</button>\
  </header>',
        cols: [
            {
                textAlign: 'center',
                values:values
            }
        ]
    });



   //点击添加

    $('.add_img').bind('click',function () {
        index++;
        obj.list.push({'id':index,'status':'0','num':'','value':''});
        var html = template('bucket_detail',obj);
        document.getElementById('bucket_detail_content').innerHTML = html;

        //添加时 只让最后一个添加的能被删除
        $('.del_text').addClass('none');
        $('#del'+index).removeClass('none');

        $(".del").bind("click",function(){
            var id = parseInt(this.getAttribute('data-id'));
            $('#bucket'+id).remove();
            //删除列表中对应的项
            for(var i=0;i<obj.list.length;i++){
                if(obj.list[i].id == id){
                    obj.list.splice(i, 1);
                    console.log(obj.list)
                }
            }
            index--;
            $('#del'+index).removeClass('none');
            $.toast('删除成功');

        });

        $('.bucket_category').bind('click',function () {
            var change_status = 0;
            var id= parseInt(this.getAttribute('data-id'));
            $("#bucket_category_id_value"+id).change(function () {
                change_status = 1;
                if (dataArr.length > 0) {
                    var status = 0;
                    for (var i = 0; i < dataArr.length; i++) {
                        if (dataArr[i]['title'] == $("#bucket_category_id_value"+id).val()) {
                            title1 = dataArr[i]['title'];
                            status = dataArr[i]['bucket_category_id'];
                            $("#bucket_category_id"+id).val(dataArr[i]['bucket_category_id']);
                            //给obj.list 对应序列添加数据
                        }
                    }
                    for(var i=0;i<obj.list.length;i++){
                        if(obj.list[i].id == id){
                            obj.list[i].status =status;
                            obj.list[i].value =title1;
                            break;
                        }
                    }
                }
                console.log(obj.list)
            });

            if(change_status === 0){
                var value_end = $('#bucket_category_id_value'+id).val();
                if(isEmpty(value_end)){
                    status = dataArr[0]['bucket_category_id'];
                    $("#bucket_category_id"+id).val(dataArr[0]['bucket_category_id']);
                    $('#bucket_category_id_value'+id).val(dataArr[0]['title']);
                    obj.list[id-1].status =dataArr[0]['bucket_category_id'];
                    obj.list[id-1].value =dataArr[0]['title'];
                }

            }
        })

        //给默认的第一个input 添加监听事件
        $('.nums').bind('click',function () {
            $('.bucket-content input').addClass('gray_bor');
            $('.bucket-content input').removeClass('red_bor');
            var id= parseInt(this.getAttribute('data-id'));
            $('#nums'+id).bind('input propertychange', function() {
                var value = $(this).val();
                for(var i=0;i<obj.list.length;i++){
                    if(obj.list[i].id == id){
                        obj.list[i].num =value;
                        break;
                    }
                }
            });
        });

        $(".picker").picker({
            toolbarTemplate: '<header class="bar bar-nav">\
  <button class="button button-link pull-left  close-picker" style="color:#0894ec ;">取消</button>\
  <button class="button button-link pull-right close-picker" style="color:#0894ec ;">确定</button>\
  </header>',
            cols: [
                {
                    textAlign: 'center',
                    values:values
                }
            ]
        });

    })

};
getData(worker_select_data, {aid: work_info.aid, sid: work_info.sid,wid: work_info.wid,mid:mid,from:'wx'}, success);


var ajaxOption = {
    url: worker_recover_bucket,
    method: 'POST',
    confirm:true,
    confirm_title:'数据确认',
    confirm_content:['numValue'],//用[]包起来表示该值是函数，不包则为字符串
    data:{aid: work_info.aid, sid:work_info.sid, wid: work_info.wid,mid:mid},
    success: function (res) {
        setTimeout(function () {
            redirect('import.html');
        },2000);
    }
};

checkform($('#myForm'), ajaxOption);



//验证表单并处理登录交互
/*function numValue() {
    num = $('#nums').val();
    num1 = $('#nums1').val();
    num2 = $('#nums2').val();
    if(title && num){
        showcontent = title +' '+ num +' '+'个';
    }
    if(title1 && num1){
        showcontent1 = title1 +' '+ num1 +' '+'个';
    }
    if(title2 && num2){
        showcontent2 = title2 +' '+ num2 +' '+'个';
    }
    var html = '<p>'+showcontent+'</p>'+'<p>'+showcontent1+'</p>'+'<p>'+showcontent2+'</p>';
    return html;
}*/

//验证表单并处理登录交互
function numValue() {
    num = $('#nums').val();
    if(title && num){
        showcontent = title +' '+ num +' '+'个';
    }
    var html = '<p>'+showcontent+'</p>';
    for(var i=0;i<obj.list.length;i++){
        var num = $('#nums'+obj.list[i].id).val();
        if(!isEmpty(num)){
            showcontent=obj.list[i].value + ' '+ num + ' '+'个';
            html +='<p>'+showcontent+'</p>';
        }
    }
    return html;
}



